/*
 * Copyright (C) 2010 恒康信息科技有限公司
 * 版权所有
 */
package com.jjmmbb.db.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jjmmbb.anydolist.Comm;
import com.jjmmbb.anydolist.R;
import com.jjmmbb.anydolist.Util;
import com.jjmmbb.db.pojo.Enums;
import com.jjmmbb.db.pojo.Item;

public class ManageAdapter extends BaseAdapter {
	Context context;
	ArrayList<Item> list;

	public ManageAdapter(Context context, ArrayList<Item> list) {
		this.context = context;
		if (null == list) {
			list = new ArrayList<Item>();
		}
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Item item = list.get(position);
		TextView textView;
		if (null == convertView) {
			textView = new TextView(context);
			textView.setPadding(10, 10, 10, 10);
			textView.setTextAppearance(context, R.style.Medium);
			textView.setBackgroundResource(R.drawable.item_doing);
			textView.setTextColor(Color.BLACK);
			textView.setSingleLine();
			textView.setEllipsize(TextUtils.TruncateAt.END);
		} else {
			textView = (TextView) convertView;
		}

		String msg = "";
		int indexEnter = item.title.indexOf("\n");
		String title = item.title;
		if (indexEnter > 0) {
			title = title.substring(0, indexEnter);
		}		
		if (!Comm.hide_team && !Util.isNull(item.team)) {
			msg += "[" + item.team + "] ";
		}
		msg += title;
		textView.setText(msg);

		switch (item.type) {
		case Enums.Item_Doing:
			if (item.typeAlarm <= Enums.Alarm_No || item.typeAlarm == 0) {
				textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			} else {
				textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.icon_doing, 0);
			}
			// textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
			// R.drawable.icon_doing, 0);
			break;
		case Enums.Item_Finish:
			textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_finish, 0);
			break;
		case Enums.Item_Delete:
			textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_delete, 0);
			break;
		}

		if (item.isSelect) {
			textView.setTextColor(Color.YELLOW);
		} else {
			textView.setTextColor(Color.BLACK);
		}
		return textView;
	}

}
